#!Paper Counter Version 1
#!By Xinnian Guo, Yudong Yang, Huiwen Ye
#!@2021.01.20
import cv2
import os
import math
import RPi.GPIO as GPIO
import time
import numpy as np
import St1
import St2
import St3
import St4
import St5
import St6
import St7
from collections import Counter
from ctypes import *
from pygame import mixer

libad=cdll.LoadLibrary("/home/pi/Bookshelf/count/test_C.so")
mixer.init()
b=0
q=0
k=0
T1=0
T2=0
time1=0
time2=0
time3=0
time4=0
Din1 = 17
Din2 = 26
LedPin =12
freq = 1000
L1 = 6
L2 = 13
L3 = 19
L4 = 5
C1 = 1
C2 = 16
C3 = 20
C4 = 21
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(Din1, GPIO.IN)
GPIO.setup(Din2, GPIO.IN)
GPIO.setup(LedPin, GPIO.OUT)
GPIO.setup(L1, GPIO.OUT)
GPIO.setup(L2, GPIO.OUT)
GPIO.setup(L3, GPIO.OUT)
GPIO.setup(L4, GPIO.OUT)
GPIO.setup(C1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(C2, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(C3, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(C4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) 
g_col_list = [C1, C2, C3, C4]
def readLine(row_num, characters):
    GPIO.output(row_num, GPIO.HIGH)
    key = None
    for col in g_col_list:
        if GPIO.input(col) == 1:
            key = characters[ g_col_list.index(col) ]
            break
    GPIO.output(row_num, GPIO.LOW)
    return key
while True: 
    s1=GPIO.input(26)
    cv2.namedWindow("UI",cv2.WINDOW_FREERATIO)
    cv2.startWindowThread()
    cv2.setWindowProperty("UI", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
    UI=cv2.imread("/home/pi/Bookshelf/count/UI_Chn.jpg")
    UI0=cv2.imread("/home/pi/Bookshelf/count/UI_Chn.jpg")
    if b==0:
        cv2.imshow("UI",UI)
        cv2.waitKey(1)
    k1=readLine(L1, ["1","2","3","A"])
    k2=readLine(L2, ["4","5","6","0"])
    k3=readLine(L3, ["7","8","9","C"])
    time.sleep(0.1)
    if (s1==0 and k==0) or (k1=="A"):
        k=k+1
        if q==0:
            GPIO.output(12,1)
            q=q+1
            time.sleep(0.1)
        GPIO.output(L1, GPIO.HIGH)
        GPIO.output(L2, GPIO.HIGH)
        GPIO.output(L3, GPIO.HIGH)
        GPIO.output(L4, GPIO.HIGH)
        cap = cv2.VideoCapture(0)
        width = 1900
        height =1500
        Appro_Num=35
        cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
        cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
        cap.set(cv2.CAP_PROP_FOURCC,cv2.VideoWriter.fourcc("M","J","P","G"))
        ret,frame = cap.read()
        cap.release()
        for i in range (1,5):
            r1=240*i
            r2=240*i+4
            r3=600
            r4=1605
            I20 = cv2.cvtColor(frame[r1:r2,r3:r4,:], cv2.COLOR_BGR2GRAY)
            sum_I0=St2.sum_col(I20)
            sum_I20=St3.filter_G(sum_I0)
            diff_I20=St4.diff_fb(sum_I20)
            pp=St5.p_count(diff_I20,Appro_Num)
            locals()["p"+str(i)]=pp
        p90=[p1[2],p1[3],p2[2],p2[3],p3[2],p3[3],p4[2],p4[3]]
        p91=Counter(p90).most_common(1)[0][0]
        p92=Counter(p1+p2+p3+p4).most_common(1)[0][0]
        if p91==p92:
            p9=p92
        else:
            p9=p91
        St7.switch(p9)
        font = cv2.FONT_HERSHEY_SIMPLEX
        UI = cv2.putText(UI, str(p9), (245, 635), font, 15, (255, 255, 255), 22)
        cv2.imshow("UI",UI)
        cv2.waitKey(1)
        cv2.waitKey(1)
        b=libad.ads(b)
        GPIO.output(L1, GPIO.LOW)
        GPIO.output(L2, GPIO.LOW)
        GPIO.output(L3, GPIO.LOW)
        GPIO.output(L4, GPIO.LOW)
        time1=time.time()
    if (s1==1):
        k=0
    if (q!=0):
        time2=time.time()
        T1=time2-time1
        if T1>=3:
            GPIO.output(12,0)
            q=0
            cv2.imshow("UI",UI0)
            cv2.waitKey(1)
            cv2.waitKey(1)
    if (k3=="8") :
        time3=time.time()
        while (readLine(L3, ["7","8","9","C"])=="8"):
            time4=time.time()
            T2=time4-time3
            if T2>=4:
                GPIO.output(12,0)
                cv2.destroyAllWindows()
        break
        break
    if (k3=="C"):
        time5=time.time()
        while (readLine(L3, ["7","8","9","C"])=="C"):
            time6=time.time()
            T3=time6-time5
            if T3>=5:
                GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
                cmd="sudo shutdown -h now"
                os.system(cmd)
